﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace BovespaMaintainabilityRules
{
    public sealed class Rule_FileMustHaveOnlyOnePublicType : BaseAddIn.Rules.BaseRuleAddIn
    {
        public Rule_FileMustHaveOnlyOnePublicType()
            : base("BovespaFileMustHaveOnlyOnePublicType",
                   "BovespaMaintainabilityRules.BovespaMaintainabilityRules",
                   typeof(Rule_FileMustHaveOnlyOnePublicType).Assembly)
        {
        }

        protected sealed override void Check(CodeNamespace namespaceElement)
        {
            CodeClass2 classe = null;
            bool variasClassesPublicas = false;

            if (namespaceElement.Children != null)
            {
                for (int j = 1; j <= namespaceElement.Children.Count; j++)
                {
                    if (namespaceElement.Children.Item(j).Kind == vsCMElement.vsCMElementClass)
                    {
                        classe = (CodeClass2)namespaceElement.Children.Item(j);

                        if (!classe.StartPoint.Parent.Parent.FullName.Contains(".Designer.")
                            && classe.Access == vsCMAccess.vsCMAccessPublic)
                        {
                            if (variasClassesPublicas)
                            {
                                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                                 classe.StartPoint.Line,
                                                 classe.StartPoint.DisplayColumn,
                                                 "FileMustHaveOnlyOnePublicTypeResolution",
                                                 classe.Name);
                            }

                            variasClassesPublicas = true;
                        }
                    }
                }
            }
        }
    }
}
